home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / filter.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  6KB  |  196 lines

  1. #include <stdlib.h>
  2. #include "global.h"
  3. #include "filter.h"
  4. #include "simple.h"
  5.  
  6. #include <stdio.h>
  7.  
  8. int w[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  9.           // color - weight
  10. int used1 = 0;
  11. ////////////////////////
  12. void filter(int** kernel, rect coord)
  13.     {
  14.     FILE* file;                                // We can not immediately show
  15.     if((file = fopen("_filter", "w+b")) == NULL)  // changes on screen, to avoid
  16.     return;                                // side effects we use file
  17.     int sum, ksum;
  18.     for(int y = coord.origin.Y + 1; y < coord.corner.Y - 1; y++)
  19.     for(int x = coord.origin.X + 1; x < coord.corner.X - 1; x++)
  20.         {
  21.         sum = 0, ksum = 0;
  22.         for(int j = 0; j < 3; j++)
  23.         for(int i = 0; i < 3; i++)
  24.             {
  25.             sum = sum + kernel[i][j]
  26.             * getpixel(x + i - 1, y + j - 1);
  27.             ksum = ksum + kernel[i][j];
  28.             }
  29.         fputc(sum / ksum, file);
  30.         }
  31.     rewind(file);
  32.     for(y = coord.origin.Y + 1; y < coord.corner.Y - 1; y++)
  33.     for(int x = coord.origin.X + 1; x < coord.corner.X - 1; x++)
  34.         putpixel(x, y, fgetc(file));    // now redraw work area
  35.     fclose(file);
  36.     unlink("_filter");
  37.     }
  38. ////////////////////////
  39. inline int mini(int x, int y)
  40.     {
  41.     return (x > y) ? y : x;
  42.     }
  43. ////////////////////////
  44. void add(int color)
  45.     {
  46.     for(int i = 0; i < used1; i++)
  47.     {
  48.     if(w[i] == color)
  49.         return;
  50.     }
  51.     w[used1] = color;
  52.     used1++;
  53.     }
  54. ////////////////////////
  55. int near_color(int color)
  56.     {
  57.     register int i;
  58.     register int delta = 16;
  59.     register int d;
  60.     register int s;
  61.     for(i = 0; i < used1; i++)
  62.     {
  63.     if((d = mini(abs(color - w[i]), delta)) != delta)
  64.         {
  65.         delta = d;
  66.         s = i;
  67.         }
  68.     }
  69.     return w[s];
  70.     }
  71. ////////////////////////
  72. void color_filter(int* kernel, rect coord)
  73.     {
  74.     FILE* file;                                // We can not immediately show
  75.     if((file = fopen("_filter", "w+b")) == NULL)  // changes on screen, to avoid
  76.     return;                                // side effects we use file
  77.     int sum, ksum, color;
  78.  
  79.     for(int y = coord.origin.Y + 1; y < coord.corner.Y - 1; y++)
  80.     for(int x = coord.origin.X + 1; x < coord.corner.X - 1; x++)
  81.         {
  82.         sum = 0, ksum = 0; used1 = 0;
  83.         for(int j = 0; j < 3; j++)
  84.         for(int i = 0; i < 3; i++)
  85.             {
  86.             sum = sum + kernel[i + 3 * j]
  87.             * (color = getpixel(x + i - 1, y + j - 1));
  88.             ksum = ksum + kernel[i + 3 * j];
  89.             add(color);
  90.             }
  91.         fputc(near_color(sum / ksum), file);
  92.         }
  93.     rewind(file);
  94.     for(y = coord.origin.Y + 1; y < coord.corner.Y - 1; y++)
  95.     for(int x = coord.origin.X + 1; x < coord.corner.X - 1; x++)
  96.         putpixel(x, y, fgetc(file));    // now redraw work area
  97.     fclose(file);
  98.     unlink("_filter");
  99.     }
  100. ////////////////////////
  101. void dither_BW(int* threshold, rect coord, loc dim)
  102.     {
  103.     int max_color = getmaxcolor();
  104.     for(int y = coord.origin.Y; y < coord.corner.Y; y++)
  105.     {
  106.     int j = ((y - 1) % dim.Y) + 1;
  107.     for(int x = coord.origin.X; x < coord.corner.X; x++)
  108.         {
  109.         int i = ((x - 1) % dim.X) + 1;
  110.         if(getpixel(x, y) > threshold[i + j * dim.X] * max_color / (dim.X * dim.Y))
  111.         putpixel(x, y, global_i[6]);
  112.         else
  113.         putpixel(x, y, global_i[7]);
  114.         }
  115.     }
  116.     }
  117. //////////////////////////
  118. void dither_BW(int* threshold, rect coord, loc dim,
  119.               GrafBuffer* buf)
  120.     {
  121.     int max_color = getmaxcolor();
  122.  
  123.     int end_bound = coord.corner.Y / buf->bound_size.Y + 1;
  124.     int start_bound = coord.origin.Y / buf->bound_size.Y;
  125.  
  126.     for(int bound = start_bound; bound < end_bound; bound++)
  127.     {
  128.     buf->get_bound(bound);
  129.     int s_y = (bound > start_bound) ? 0
  130.           : coord.origin.Y - bound * buf->bound_size.Y;  // y coord inside image
  131.     int e_y = (bound < end_bound - 1) ? buf->bound_size.Y
  132.           : coord.corner.Y - bound * buf->bound_size.Y;
  133.     for(int y = s_y; y < e_y; y++)
  134.         {
  135.         int j = (y % dim.Y) + 1;
  136.         for(int x = coord.origin.X; x < coord.corner.X; x++)
  137.         {
  138.         int i = (x % dim.X) + 1;
  139.         if(image_get_pixel(buf->image, loc(x, y), buf->bitpx,
  140.             buf->nplanes) > threshold[i + dim.X * j] * max_color
  141.                              / (dim.X * dim.Y))
  142.             image_put_pixel(buf->image, loc(x, y), global_i[6],
  143.             buf->bitpx, buf->nplanes);
  144.         else
  145.             image_put_pixel(buf->image, loc(x, y), global_i[7],
  146.             buf->bitpx, buf->nplanes);
  147.         }
  148.         }
  149.  
  150.     buf->put_bound(bound);
  151.     }
  152.     }
  153. //////////////////////////
  154. void error_dither(rect work)
  155.     {
  156.     for(int y = work.origin.Y; y < work.corner.Y; y++)
  157.     for(int x = work.origin.X; x < work.corner.X; x++)
  158.         {
  159.             if(random(16) > getpixel(x, y))
  160.             putpixel(x, y, global_i[6]);
  161.             else
  162.         putpixel(x, y, global_i[7]);
  163.         }
  164.     }
  165. ////////////////////
  166. void error_dither_buf(GrafBuffer* buf, rect coord)
  167.     {
  168.     int end_bound = coord.corner.Y / buf->bound_size.Y + 1;
  169.     int start_bound = coord.origin.Y / buf->bound_size.Y;
  170.  
  171.     for(int bound = start_bound; bound < end_bound; bound++)
  172.     {
  173.     buf->get_bound(bound);
  174.     int s_y = (bound > start_bound) ? 0
  175.           : coord.origin.Y - bound * buf->bound_size.Y;  // y coord inside image
  176.     int e_y = (bound < end_bound - 1) ? buf->bound_size.Y
  177.           : coord.corner.Y - bound * buf->bound_size.Y;
  178.     for(int y = s_y; y < e_y; y++)
  179.         for(int x = coord.origin.X; x < coord.corner.X; x++)
  180.         {
  181.         if(image_get_pixel(buf->image, loc(x, y), buf->bitpx,
  182.             buf->nplanes) < random(16))
  183.             image_put_pixel(buf->image, loc(x, y), global_i[6],
  184.             buf->bitpx, buf->nplanes);
  185.         else
  186.             image_put_pixel(buf->image, loc(x, y), global_i[7],
  187.             buf->bitpx, buf->nplanes);
  188.         }
  189.  
  190.     buf->put_bound(bound);
  191.     }
  192.     }
  193.  
  194.  
  195.  
  196.